package com.data.algorithm.sort;

/**
 * 冒泡排序
 *
 * @author wangjie
 * @version V1.0
 * @date 2020/3/6
 */
public class BubbleSort {

    public static void main(String[] args) {
        int[] arr = new int[80000];
        for(int i = 0; i < 80000; ++i) {
            arr[i] = (int)(Math.random() * 8000000.0D);
        }

        Long startTime = System.currentTimeMillis();
        System.out.println("开始排序");
        bubbleSort(arr);
        Long endTime = System.currentTimeMillis()- startTime;

        System.out.println("排序耗时：" + endTime+"毫秒");
        System.out.println("排序耗时：" + endTime/1000+"秒");

    }


    /**
     * 冒泡排序
     * @param arr
     */
    public static void bubbleSort(int[] arr) {

        int temp;
        boolean flag = false;

        for(int i = 0; i < arr.length - 1; ++i) {
            for(int j = 0; j < arr.length - 1 - i; ++j) {
                if (arr[j] > arr[j + 1]) {
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            if (!flag) {
                break;
            }
            flag = false;
        }

    }
}
